<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use Mail;

class LoginController extends Controller
{
    public function login()
    {
        return view('admin.login');
    }

    // 后台登陆 
    public function dologin(Request $request)
    {
        $user = User::where('username',$request->username)->first();
        if(empty($user)) {
            return back()->with('info','用户名不存在');
        }
        if(Hash::check($request->password, $user->password)) {
            //写入session
            $request->session()->put('id',$user->id);
            if (!$user->status==1) {
                return back()->with('info','对不起,您的权限不足,请联系管理员');
            }else{
                return redirect('/admin');
            }
        }else{
            return back()->with('info','用户名或者密码不正确');
        }
    }

    // 后台退出
    public function logout(Request $request)
    {
        $request->session()->forget('id');
        return redirect('/admin/login');
    }
    
    // 前台注册
     public function register()
    {
        return view('home.user.register');
    }

    // 前台注册
     public function doregister(Request $request)
    {
        //表单验证
        $this->validate($request, [
            'username' => 'required|regex:/^\w{8,18}$/',
            'password' => 'required|regex:/^\S{6,30}$/|same:repassword'
        ],[
            'username.required' => '用户名不能为空',
            'username.regex' => '用户名格式不正确',
            'password.required' =>'密码不能为空',
            'password.regex'=>'密码格式不正确',
            'password.same'=>'两次密码不一致'
        ]);

        // 验证码验证
        $userInput = \Request::get('captcha');
        
        if (\Session::get('milkcaptcha') == $userInput) {
           //用户输入验证码正确
           
        // 实例化对象,获取参数
            $user = new User;
            $user -> username = $request ->username;
            $user -> password = Hash::make($request ->password);
            $user -> email = $request ->email;
            $user -> phone = $request ->phone;

            if ($user->save()) {
                // 发送邮件
                Mail::send('emails.register', ['user' => $user], function ($m) use ($user) { $m->from('18600796490@163.com', 'Vancl'); 
                    $m->to($user->email, $user->username)->subject('凡客诚品注册提醒'); });
                return view('home.user.registerremind');
            } else {
                return back()->with('error','注册失败');
            }
        
        } else {
           //用户输入验证码错误
            return back()->with('error','验证码输入有误');
        }
    }

    // 激活账户
     public function jihuo(Request $request)
     {
        // 读取用户
        $user = User::findOrFail($request->id);
        $user ->status =1;
        if($user->save()){
            return redirect('/source')->with('success','恭喜您,激活成功!!!');
        }else{
            return redirect('/source')->with('error','对不起,激活失败!!!');
        };
     }

    // 前台登陆
     public function hmlogin()
    {
        return view('home.user.login');
    }

    // 前台登陆 
    public function dohmlogin(Request $request)
    {
        $user = User::where('username',$request->username)->first();
        if(empty($user)) {
            return back()->with('info','用户名不存在');
        }
        if(Hash::check($request->password, $user->password)) {
            //写入session
            $request->session()->put('id',$user->id);
             return redirect('/source');
            
        }else{
            return back()->with('info','用户名或者密码不正确');
        }
    }

}
